﻿using System.Collections.Generic;
using System.Linq;
using Orchard.Caching;
using Orchard.Environment;
using Orchard.Environment.Extensions;
using Orchard.UI.Navigation;

namespace SchoolPointe.Menus.Services
{
    /// <summary>
    /// Thanks http://orchardproject.net/gallery/List/Modules/Orchard.Module.Szmyd.Orchard.Modules.Menu
    /// </summary> 
    public class NavigationProviderFactory : INavigationProviderFactory
    {
        private readonly Work<IMenuService> _menuService;
        private readonly IEnumerable<INavigationProvider> _providers;

        public NavigationProviderFactory(Work<IMenuService> menuService, IEnumerable<INavigationProvider> providers)
        {
            _menuService = menuService;
            _providers = providers;
        }

        #region Implementation of INavigationProviderFactory

        /// <summary>
        /// Gets providers for the dynamically created menus. 
        /// </summary>
        public IEnumerable<INavigationProvider> Providers
        {
            get
            {
                foreach (var p in _providers)
                {
                    yield return p;
                }
                foreach (var p in _menuService.Value.GetNavigationProviders())
                    yield return p;
            }
        }
        #endregion
    }        
}